package com.luoran.bartalks.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.luoran.bartalks.core.AppUser;
import com.luoran.bartalks.dao.IBtPlanBizDao;
import com.luoran.bartalks.dao.IBtPlanBizPicDao;
import com.luoran.bartalks.dao.IBtRuleRecordDao;
import com.luoran.bartalks.dao.IBtRuleRecordPicDao;
import com.luoran.bartalks.entity.biz.BtPlanBiz;
import com.luoran.bartalks.entity.biz.BtPlanBizPic;
import com.luoran.bartalks.entity.biz.BtPlanDetails;
import com.luoran.bartalks.entity.biz.BtProject;
import com.luoran.bartalks.entity.biz.BtRuleRecord;
import com.luoran.bartalks.entity.biz.BtRuleRecordPic;
import com.luoran.bartalks.service.IBtOperationLogService;
import com.luoran.bartalks.service.IBtPlanDetailsService;
import com.luoran.bartalks.service.IBtProjectService;
import com.luoran.bartalks.service.IOfflineDataService;
import com.luoran.bartalks.utils.EnumOperationType;
import com.luoran.bartalks.utils.ImgBase64Util;
import com.luoran.bartalks.utils.UUID;

@Service
@Transactional()
public class OfflineDataService implements IOfflineDataService {

	@Autowired
	private IBtPlanDetailsService planDetailsService;

	@Autowired
	private IBtProjectService projectService;

	@Autowired
	private IBtOperationLogService operationLogService;

	@Autowired
	private IBtPlanBizDao planBizDao;

	@Autowired
	private IBtPlanBizPicDao planBizPicDao;

	@Autowired
	private IBtRuleRecordDao ruleRecordDao;

	@Autowired
	private IBtRuleRecordPicDao ruleRecordPicDao;

	@Value("${image.path}")
	private String imagePath;

	@Value("${image.url}")
	private String imageUrl;

	@Override
	public void loadOrUnloadInsertsBatch(List<BtPlanBiz> loadList, List<BtPlanBiz> unloadList,
			List<BtPlanBizPic> planBizPic, String projectId, String planId, String planPositionId) {
		if (loadList != null && loadList.size() != 0) {
			planBizDao.insertBatch(loadList);
			// 保存操作日志
			operationLogService.saveOperationRecord(AppUser.get().getId(), AppUser.get().getAlias(),
					AppUser.get().getCompanyId(), EnumOperationType.LOADING.getValue());
			// 项目计划自启动
			int started = planDetailsService.getStartedById(planId);
			if (started == 1) {
				BtPlanDetails plan = new BtPlanDetails();
				plan.setId(planId);
				plan.setStarted(0);
				planDetailsService.updatePlan(plan);
				BtProject project = new BtProject();
				project.setId(planDetailsService.getItemById(plan.getId()).getProjectId());
				project.setStarted(0);
				projectService.updateTemplateById(project);
			}
		}
		if (unloadList != null && unloadList.size() != 0) {
			planBizDao.insertBatch(unloadList);
			// 保存操作日志
			operationLogService.saveOperationRecord(AppUser.get().getId(), AppUser.get().getAlias(),
					AppUser.get().getCompanyId(), EnumOperationType.UNLOAD.getValue());
			// 项目计划自启动
			int started = planDetailsService.getStartedById(planId);
			if (started == 1) {
				BtPlanDetails plan = new BtPlanDetails();
				plan.setId(planId);
				plan.setStarted(0);
				planDetailsService.updatePlan(plan);
				BtProject project = new BtProject();
				project.setId(planDetailsService.getItemById(plan.getId()).getProjectId());
				project.setStarted(0);
				projectService.updateTemplateById(project);

			}
		}
		if (planBizPic != null && planBizPic.size() != 0) {
			for (BtPlanBizPic btPlanBizPic : planBizPic) {
				if (!"".equals(btPlanBizPic.getBASE64())) {
					String FileName = UUID.get();
					String deposition = imagePath + "/" + FileName + ".jpg";
					String picUrl = imageUrl + "/" + FileName + ".jpg";
					btPlanBizPic.setForcedPicUrl(picUrl);
					// boolean generateImagetest =
					// ImgBase64Util.GenerateImage(btPlanBizPic.getBASE64(),
					// "e:\\bartalkstest\\new" + UUID.get() + ".jpg");
					boolean generateImage = ImgBase64Util.GenerateImage(btPlanBizPic.getBASE64(), deposition);
					if (!generateImage) {
						throw new RuntimeException("BASE64转图片失败！,请查看日志！");
					}
					planBizPicDao.insert(btPlanBizPic);
				}
			}
		}
	}

	@Override
	public void ruleRecordListInsertsBatch(List<BtRuleRecord> ruleRecordList, List<BtRuleRecordPic> ruleRecordPics) {
		if (ruleRecordList != null && ruleRecordList.size() != 0) {
			ruleRecordDao.insertBatch(ruleRecordList);
		}
		if (ruleRecordPics != null && ruleRecordPics.size() != 0) {
			for (BtRuleRecordPic btRuleRecordPic : ruleRecordPics) {
				if (!"".equals(btRuleRecordPic.getBASE64())) {
					String FileName = UUID.get();
					String deposition = imagePath + "/" + FileName + ".jpg";
					String picUrl = imageUrl + "/" + FileName + ".jpg";
					btRuleRecordPic.setRulePicUrl(picUrl);
					// boolean generateImagetest =
					// ImgBase64Util.GenerateImage(btRuleRecordPic.getBASE64(),
					// "e:\\bartalkstest\\new" + UUID.get() + ".jpg");
					boolean generateImage = ImgBase64Util.GenerateImage(btRuleRecordPic.getBASE64(), deposition);
					if (!generateImage) {
						throw new RuntimeException("BASE64转图片失败！,请查看日志！");
					}
					ruleRecordPicDao.insert(btRuleRecordPic);
				}
			}
		}

	}

	/**
	 * 保留接口，异常上报功能重构备用
	 */
	@Override
	public void exceptionInfoListInsertsBatch(List<BtPlanBiz> exceptionInfoList) {
		// TODO Auto-generated method stub

	}

}
